unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Image1: TImage;
    ScrollBar1: TScrollBar;
    Timer1: TTimer;
    BitBtn1: TBitBtn;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);

  private
    procedure Draw;
  public
    { Public declarations }
  end;

const
  N=30;
  w=30;
  h=10;
var
  Form1: TForm1;
  i, j, t, a, k: integer;
  arr, turn: array of integer;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (turn[j]>turn[j+1]) then
  begin
    t:=turn[j+1];
    turn[j+1]:=turn[j];
    turn[j]:=t;
  end;
  Draw;
  j:=j+1;
  if (j=N-i-1) then
  begin
    j:=0;
    i:=i+1;
    if (i=N-1) then
      timer1.Enabled:=false;
  end;
end;

procedure TForm1.Draw;
begin
  Image1.Canvas.Rectangle(Image1.ClientRect);
  for k:=0 to N-1 do
  begin
    if (k=j) or (k=j+1) then
    begin
      Image1.Canvas.Brush.Color:=clPurple;
      Image1.Canvas.Rectangle(w*k, 0, w*(k+1), h*turn[k]);
      Image1.Canvas.Brush.Color:=clWhite;
    end else begin
      Image1.Canvas.Rectangle(w*k, 0, w*(k+1), h*turn[k]);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  randomize;

  setlength(arr, N);
  setlength(turn, N);
  for i:=0 to N-1 do
  begin

    arr[i]:=random(60);
  end;
  for i:=0 to N do
    turn[i]:=arr[i];
  Draw;
  i:=0;
  j:=0;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Timer1.Enabled:=True;
  i:=0;
  for i:=0 to N do
    turn[i]:=arr[i];
  Draw;
  i:=0;
  j:=0;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Timer1.Interval:=ScrollBar1.Position;
end;

end.

